package com.zzk.www.tool.common;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by zzk on 2017/12/22.
 */
public class DateUtil {

    public static final int MINITUE_SECONDS = 60;
    public static final int MINITUE_MILLIS = 60000;
    public static final int MINITUE_5_SECONDS = 300;
    public static final int MINITUE_10_SECONDS = 600;
    public static final int MINITUE_5_MILLIS = 300000;
    public static final int HOUR_SECONDS = 3600;
    public static final int HOUR_MILLIS = 3600000;
    public static final int DAY_SECONDS = 86400;
    public static final int DAY_MILLIS = 86400000;
    public static final int MONTH_SECONDS = 2592000;
    public static final int MONTH_MILLIS = -1702967296;

    public DateUtil() {
    }

    public static String getFmtYMDNoSymbol(long timeMillis) {
        return (new SimpleDateFormat("yyyyMMdd")).format(new Date(timeMillis));
    }

    public static String getFmtyMdHmsNoSymbol(long timeMillis) {
        return (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date(timeMillis));
    }

    public static String getFmtyMdHmsSSSNoSymbol(long timeMillis) {
        return (new SimpleDateFormat("yyyyMMddHHmmssSSS")).format(new Date(timeMillis));
    }

    public static String getFmtYMDHMS(long timeMillis) {
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(timeMillis));
    }

    public static String getFmtYMDHM(long timeMillis) {
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date(timeMillis));
    }

    public static String getFmtYMD(long timeMillis) {
        return (new SimpleDateFormat("yyyy-MM-dd")).format(new Date(timeMillis));
    }

    public static String getFmt(long timeMillis, String format) {
        return (new SimpleDateFormat(format)).format(new Date(timeMillis));
    }

    public static Date getDate(String time, String format) {
        try {
            return (new SimpleDateFormat(format)).parse(time);
        } catch (ParseException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static Date getDateFmtYMDHMS(String time) {
        return getDate(time, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date getDateFmtYMDHM(String time) {
        return getDate(time, "yyyy-MM-dd HH:mm");
    }

    public static Date getDateFmtYMDH(String time) {
        return getDate(time, "yyyy-MM-dd HH");
    }

    public static Date getDateFmtYMD(String time) {
        return getDate(time, "yyyy-MM-dd");
    }

    public static Date getDateAfter(Date dt, int day) {
        Calendar curr = Calendar.getInstance();
        curr.setTime(dt);
        curr.add(5, day);
        return curr.getTime();
    }

    public static Date getTimeAfter(Date dt, int field, int num) {
        Calendar curr = Calendar.getInstance();
        curr.setTime(dt);
        curr.add(field, num);
        return curr.getTime();
    }

    public static Date getMonthAfter(Date dt, int day) {
        Calendar curr = Calendar.getInstance();
        curr.setTime(dt);
        curr.add(2, day);
        return curr.getTime();
    }

    public static boolean isDateAfter(Date dt, int day) {
        Date copDate = getDateAfter(dt, day);
        return !copDate.before(new Date());
    }

    public static Date getDateFirstSecond(Date date) {
        if(date == null) {
            return null;
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.set(11, 0);
            c.set(12, 0);
            c.set(13, 0);
            c.set(14, 0);
            return c.getTime();
        }
    }

    public static Date getDateLastSecond(Date date) {
        if(date == null) {
            return null;
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.set(11, 23);
            c.set(12, 59);
            c.set(13, 59);
            c.set(14, 0);
            return c.getTime();
        }
    }

    public static String remainTime(Date beginTime, Date endTime) {
        if(beginTime != null && endTime != null) {
            long seconds = (endTime.getTime() - beginTime.getTime()) / 1000L;
            return seconds / 86400L > 0L?seconds / 86400L + "天":(seconds / 3600L > 0L?seconds / 3600L + "小时":(seconds / 60L > 0L?seconds / 60L + "分钟":null));
        } else {
            return null;
        }
    }

    public static Date transGMT2CST(String gmtTime) {
        DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
        format.setTimeZone(TimeZone.getTimeZone("GMT"));

        try {
            return format.parse(gmtTime);
        } catch (ParseException var3) {
            return null;
        }
    }

    public static String transCST2GMT(Date date) {
        DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        return format.format(date);
    }

    public static Date getLastTimeOfThisWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(2);
        cal.set(7, cal.getFirstDayOfWeek() + 6);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfWeek = sdf.format(cal.getTime()) + " 23:59:59";

        try {
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date lastDate = sdf2.parse(lastDayOfWeek);
            return lastDate;
        } catch (ParseException var5) {
            var5.printStackTrace();
            return null;
        }
    }

    public static Date getFirstTimeofThisWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(2);
        cal.set(7, cal.getFirstDayOfWeek());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfWeek = sdf.format(cal.getTime()) + " 00:00:00";

        try {
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date lastDate = sdf2.parse(lastDayOfWeek);
            return lastDate;
        } catch (ParseException var5) {
            var5.printStackTrace();
            return null;
        }
    }

    public static String distanceTime(Date beginTime, Date endTime) {
        long days = 0L;
        long hours = 0L;
        long minitues = 0L;
        if(beginTime != null && endTime != null) {
            long seconds = (endTime.getTime() - beginTime.getTime()) / 1000L;
            if(seconds / 86400L > 0L) {
                days = seconds / 86400L;
            }

            if((seconds - days * 86400L) / 3600L > 0L) {
                hours = (seconds - days * 86400L) / 3600L;
            }

            if((seconds - days * 86400L - hours * 3600L) / 60L > 0L) {
                minitues = (seconds - days * 86400L - hours * 3600L) / 60L;
            }

            StringBuffer sb = new StringBuffer();
            if(days > 0L) {
                sb.append(days).append("天");
            }

            if(hours > 0L) {
                sb.append(hours).append("小时");
            }

            if(minitues > 0L) {
                sb.append(minitues).append("分");
            }

            return sb.toString();
        } else {
            return null;
        }
    }

    public static String getRangeTime(Date time) {
        if(time == null) {
            return null;
        } else {
            Date now = new Date();
            long seconds = (now.getTime() - time.getTime()) / 1000L;
            return seconds < 60L?seconds + "秒钟前":(seconds < 3600L?seconds / 60L + "分钟前":(seconds < 86400L?seconds / 60L / 60L + "小时前":getFmt(time.getTime(), "yyyy-MM-dd")));
        }
    }

    public static String getRangeTime(Date time, String format) {
        if(time == null) {
            return null;
        } else {
            Date now = new Date();
            long seconds = (now.getTime() - time.getTime()) / 1000L;
            if(seconds < 60L) {
                return seconds + "秒钟前";
            } else if(seconds < 3600L) {
                return seconds / 60L + "分钟前";
            } else if(seconds < 86400L) {
                return seconds / 60L / 60L + "小时前";
            } else {
                if(StringUtils.isEmpty(format)) {
                    format = "yyyy-MM-dd";
                }

                return getFmt(time.getTime(), format);
            }
        }
    }

    public static void main(String[] args) {
        Date time = getTimeAfter(new Date(), 12, 5);
        System.out.println(time);
        String beforeTimeString = getFmt(time.getTime(), "yyyy-MM-dd HH:mm");
        System.out.printf(beforeTimeString, new Object[0]);
    }
}
